package com.dwin.dingnuo.views;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dwin.dingnuo.R;


/**
 * 选择语言适配器
 */
public class ChooseLanguageAdapter extends BaseAdapter {
    private int selectIndex = 0; // 当前选择的是第几个语言
    private String currentLanguage; // 当前语言字符串（zh-CH,en-US）

    private Context mContext;
    private Resources mRes;
    private LayoutInflater mInflater;
    private String[] mData;

    public ChooseLanguageAdapter(Context context, String currentLanguage) {
        this.mContext = context;
        this.mRes = context.getResources();
        this.currentLanguage = currentLanguage;
        this.mData = mContext.getResources().getStringArray(R.array.arr_language);
        this.mInflater = LayoutInflater.from(mContext);
        if (currentLanguage.startsWith("zh")) {
            selectIndex = 0;
        } else {
            selectIndex = 1;
        }
    }

    @Override
    public int getCount() {
        return mData.length;
    }

    @Override
    public Object getItem(int position) {
        return mData[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_language, parent, false); //加载布局
            holder.tv_language = convertView.findViewById(R.id.tv_language);
            holder.iv_selected = convertView.findViewById(R.id.iv_selected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String bean = mData[position];
        holder.tv_language.setText(bean);
        if (position == selectIndex) {
            holder.iv_selected.setVisibility(View.VISIBLE);
        } else {
            holder.iv_selected.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tv_language;
        ImageView iv_selected;
    }

}
